package com.qhyiyun.baseweb.controller;

import com.qhyiyun.baseweb.model.CommentWall;
import com.qhyiyun.baseweb.model.Users;
import com.qhyiyun.baseweb.model.WxAuth;
import com.qhyiyun.baseweb.serivice.BaseService;
import com.qhyiyun.baseweb.util.CustomResponse;
import jakarta.servlet.http.HttpServletRequest;
import netscape.javascript.JSObject;
import org.apache.catalina.User;
import org.apache.tomcat.util.json.JSONParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.GsonJsonParser;
import org.springframework.boot.json.JsonParser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class BaseAuthCtr {
    @Autowired
    private BaseService baseService;
    private static final Logger logger = LoggerFactory.getLogger(BaseAuthCtr.class);

    @GetMapping("/wxauth")
    public String wxTokenAuth(WxAuth wxAuth) {
        logger.info("wxauth---api");
        String echostr = wxAuth.getEchostr();
        return echostr;
    }

    public String getUserAgent(Users users, HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        String ipAddress = request.getHeader("X-Forwarded-For");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            // 根据实际情况，这里可能需要特殊处理，比如当访问者和服务器位于同一网络时，getRemoteAddr()可能返回的是内网IP
        }
        users.setUserNick("tm-" + ipAddress);
        users.setUserDemand(userAgent);
        users.setOperDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
        logger.info("setUser---data--" + users.toString());
        Users users1 = baseService.save(users);
        logger.info("访问类型：" + userAgent);
        logger.info("访问Ip：" + ipAddress);
        return "User-Agent: " + userAgent;
    }

    @PostMapping("/setUser")
    public CustomResponse setUser(@RequestBody Users users, HttpServletRequest request) {
        logger.info("请求参数---" + users.toString());
        getUserAgent(users, request);
        List<Users> list = baseService.qryAll();
        int count = list.size();
        Map res = new HashMap();
        res.put("count", count);
        return CustomResponse.success("", res);
    }

    @PostMapping("/setAna")
    public CustomResponse setAna(@RequestBody CommentWall wall, HttpServletRequest request) {
        logger.info("语录----" + wall.toString());
        baseService.saveAna(wall);
        return CustomResponse.success("", wall);
    }
    @GetMapping("/getAnaAll")
    public CustomResponse getAnaAll(){
        logger.info("获取全部语录---------");
        List<CommentWall> list=baseService.qryAllAna();
        return CustomResponse.success("",list);
    }
}
